package net.hymnal.church;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

public class HymnActivity extends Activity implements View.OnClickListener {
    private static final String TAG = HymnActivity.class.getName();
    private HymnsManager hymnsManager = new HymnsManager(this);
    private HymnalType hymnalType;
    private TextView mHymnText;
    private SingleHymn mHymn;
    private MediaPlayer mediaPlayer = new MediaPlayer();
    private Settings settings;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btPrev:
                if (mHymn.getNumber() > 0) {
                    stopMediaPlayer();
                    loadHymnText(mHymn.getNumber() - 1);
                }
                break;
            case R.id.btNext:
                if (mHymn.getNumber() < Constants.BLUE_HYMNS_COUNT) {
                    stopMediaPlayer();
                    loadHymnText(mHymn.getNumber() - 1);
                }
                break;
            case R.id.btInc:
                increaseTextSize(1);
                break;
            case R.id.btDec:
                increaseTextSize(-1);
        }
    }

    void increaseTextSize(float increase) {
        float size = settings.getTextSize() + increase;
        settings.setTextSize(size);
        mHymnText.setTextSize(size);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        settings = new Settings(this);
        int themeId = settings.getTheme();
        setTheme(themeId);
        super.onCreate(savedInstanceState);
        hymnalType = HymnalType.valueOf(getIntent().getStringExtra(Constants.HYMNAL_TYPE_KEY));
        if (themeId == android.R.style.Theme_Black) {
            setContentView(R.layout.b_hymn);
        } else {
            setContentView(R.layout.w_hymn);
        }
        initControls();
        loadHymnText(getIntent().getIntExtra(Constants.HYMN_NUMBER_KEY, 1));
    }

    void initControls() {
        mHymnText = (TextView) findViewById(R.id.hymn_text_view);
        mHymnText.setTextSize(settings.getTextSize());
    }

    void loadHymnText(int number) {
        mHymn = hymnsManager.readHymn(this, hymnalType, number);
        setTitle(getString(R.string.hymn) + " №" + mHymn.getNumber());
        mHymnText.setText(mHymn.getText());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.play_melody) {
            try {
                stopMediaPlayer();
                String fileName = Constants.BLUE_MELODIES_PATH + mHymn.getMelodyFileName();

                System.out.println("Media Play: " + fileName);
                AssetFileDescriptor descriptor = getAssets().openFd(fileName);

                mediaPlayer.reset();
                mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());

                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (mHymn.getMelodyFileName() == null) {
            return true;
        }
        
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, @NotNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            finish();
            return true;
        }
        return false;
    }

    void stopMediaPlayer() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
    }
}
